Intersection of Three Sorted Arrays
Problem Descriptionβ
Visit LeetCode for the full problem description.
Solutionsβ
Solution 1: C# (Best: 136 ms)β
| Metric | Value |
|---|---|
| Runtime | 136 ms |
| Memory | 43.2 MB |
| Date | 2021-11-12 |
Solution
public class Solution {
public IList<int> ArraysIntersection(int[] arr1, int[] arr2, int[] arr3) {
Array.Sort(arr1);
Array.Sort(arr2);
Array.Sort(arr3);
int i = 0;
int j = 0;
int k = 0;
HashSet<int> result = new HashSet<int>();
while(i<arr1.Length && j<arr2.Length && k < arr3.Length)
{
int min = Math.Min(arr1[i], Math.Min(arr3[k], arr2[j]));
if(arr1[i] == arr2[j] && arr2[j] == arr3[k])
{
result.Add(arr1[i]);
i++;
j++;
k++;
}
else if(min == arr1[i]){
i++;
}
else if(arr2[j] == min){
j++;
}
else {
k++;
}
}
return result.ToList();
}
}
Complexity Analysisβ
| Approach | Time | Space |
|---|---|---|
| Solution | To be analyzed | To be analyzed |